package com.etc.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.etc.entity.Company;
import com.etc.entity.RecJoin;
import com.etc.entity.Recruitment;
import com.etc.service.CompanyService;
import com.etc.service.RecruitmentService;

@Controller
public class RecruitmentController {
  
	@Autowired
	RecruitmentService  recruitmentService;
	@Autowired
	CompanyService companyService;
	
	@GetMapping("job_fair")
	public ModelAndView select(){
		ModelAndView f1 = new ModelAndView("job_fair");
		List<Recruitment> recruitment = recruitmentService.selectRec();
		System.out.println("recruitment"+recruitment);
		f1.addObject("recruitment",recruitment);
		
		List<Recruitment> list1 = new ArrayList<Recruitment>();
		List<Recruitment> list2 = new ArrayList<Recruitment>();
		List<Recruitment> list3 = new ArrayList<Recruitment>();
		
		for(int j=0;j<recruitment.size();j++) {
			String time=recruitment.get(j).getStartTime();	
			int sj=recruitmentService.selectDate(time);
			System.out.println("time:"+time);
			System.out.println("sj:"+sj);
			if(sj<3 && sj>=0) {
				list1.add(recruitment.get(j));
			}
			if(sj<7 && sj>=0) {
				list2.add(recruitment.get(j));
			}
			if(sj<15 && sj>=0) {
				list3.add(recruitment.get(j));
			}
		}
		System.out.println("list1"+list1);
		System.out.println("list2"+list2);
		System.out.println("list3"+list3);
		f1.addObject(list1);
		f1.addObject(list2);
		f1.addObject(list3);
		return f1;
	}
	
	
	@GetMapping("job_fair2/{recName}")
	public ModelAndView job2(@PathVariable(value="recName") String recName) {
		ModelAndView f2 = new ModelAndView("job_fair2");
		List<Recruitment> RecName =recruitmentService.selectRecName(recName);
		Recruitment rec=RecName.get(0);
	    System.out.println("rec:"+rec);
	    f2.addObject("rec",rec);
	   
	    
	    List<Integer> coms = recruitmentService.selectcomId(rec.getRecId());
	    System.out.println("coms"+coms);
	
	    
	    
	    
	    for (int i=0;i<coms.size();i++) {
	    	List<Company> comps = recruitmentService.selectcomName(coms.get(i));
	    	comps.forEach(System.out::println);
	    	f2.addObject("comps",comps);
	    }    
	    
	    
	    List<Recruitment> recruitment = recruitmentService.selectRec();
	    List<Recruitment> list1 = new ArrayList<Recruitment>();
	    for(int j=0;j<recruitment.size();j++) {
			String time=recruitment.get(j).getStartTime();	
			int sj=recruitmentService.selectDate(time);
			//System.out.println("time:"+time);
			//System.out.println("sj:"+sj);
			if(sj<10 && sj>=0) {
				list1.add(recruitment.get(j));
			}
			System.out.println("list1"+list1);
			f2.addObject("list1",list1);
	    }
	    return f2;
	}
	
	@GetMapping(value="job_fair3/{comCity}")
	@ResponseBody
	public List<Recruitment> getCompanyByCity(@PathVariable("comCity") String comCity){
		System.out.println("comCity:" + comCity);
		List<Recruitment> list_company = recruitmentService.selectRecByCity(comCity);
		return list_company;
	}
	
	
}
